/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_DISCRETE_SURFACE_H
#define SX_DISCRETE_SURFACE_H

#include <sxSurface.h>
#include <sxBufferUtilities.h>
#include <sxIndiceFace.h>
#include <sxMath.h>

namespace Sx {
namespace Graphics {
namespace Objects {

/*
 * A discrete surface is formed by several discrete faces (typically triangles
 * or quads) which may be disjoint (i.e. the faces do not have to be attached).
 * Discrete surfaces also maintain a list of normals that define the surface
 * curvature through a set of unit length vectors (i.e. vertex normals and face
 * normals).
 * 
 * Vertex Normals - Represent the average of the face normals for each face
 * connected to the vertex.
 * 
 * Face Normals - Each face has a normal perpendicular to the plane it lies
 * within. If the face is not planar then it is invalid and its normal is
 * invalid.
 */
class DiscreteSurface : public Surface {
public:
	DiscreteSurface();
	DiscreteSurface(const DiscreteSurface& surface);
	virtual ~DiscreteSurface();
	
	void clear();

	/* Quick functions for adding elements to the surface */
	void addFace(const Primitives::IndiceFace& face);

	bool setFace(const Primitives::IndiceFace& face, unsigned int index);

	/* Quick functions for removing elements from the surface */
	bool remove(const Primitives::IndiceFace& face);
	bool removeFace(const Primitives::IndiceFace& face);
	bool removeFace(unsigned int index);

	/*
	 * This function takes the existing face array (which may be composed of
	 * any number of (n) sided polygons) and triangulates each face. This
	 * function returns true if the operation was successful; otherwise it
	 * returns false.
	 */
	bool triangulateFaces();

	/* 
	 * Access Functions: Direct references to the arrays are provided for
	 * performance. Generally the face list and normal arrays are used for
	 * rendering, therefore the access must be made as fast as possible.
	 */
	Util::IndexedFaceArray& getFaces();
	const Util::IndexedFaceArray& getFaces() const;

	Util::VectorArray& getNormals(Math::PrimitiveType type);
	Util::VectorArray& getVertexNormals();
	const Util::VectorArray& getVertexNormals() const;

	Util::VectorArray& getFaceNormals();
	const Util::VectorArray& getFaceNormals() const;

	unsigned int getFaceCount() const;
	unsigned int getNormalCount(Math::PrimitiveType type) const;
	unsigned int getVertexNormalCount() const;
	unsigned int getFaceNormalCount() const;

	Primitives::IndiceFace getFace(unsigned int index);
	Eigen::Vector3f getNormal(Math::PrimitiveType type, unsigned int index) const;
	Eigen::Vector3f getFaceNormal(unsigned int index) const;
	Eigen::Vector3f getVertexNormal(unsigned int index) const;

	DiscreteSurface& operator = (const DiscreteSurface& surface);

protected:
	/*
	 * An array of indexed faces. Each face is associated with (n) points in
	 * space. The (n) points is space define the boundary of one face (if the
	 * points form a convex shape according to the indices - this is not 
	 * guaranteed). All points that define a single face should be coplanar. 
	 * If all (n) points that define the boundary of a face are coplanar then
	 * the face is said to be well-formed; otherwise the face is invalid (note
	 * that for faces with 3 points (triangles) the points are always coplanar).
	 */
	Util::IndexedFaceArray faces;

	/*
	 * Array of unit length vectors that define the normal of the surface at
	 * each point. Since a vertex may belong to many faces, the vertex normal
	 * is the average of all of the adjoining face normals.
	 */
	Util::VectorArray vertexNormals;

	/*
	 * For each valid face bound by (n) points in space, the normal is defined
	 * as perpendicular to the plane in which the face resides (if the faces is
	 * not planar then the face normal is invalid).
	 */
	Util::VectorArray faceNormals;
};

}

}

}

#endif
